﻿
using System.Configuration;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Projecturo.Connectivity;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Projecturo.ProjecturoClient.Test
{
    /// <summary>
    /// Base class for all tests providing necessary instances
    /// </summary>
    public class TestBase
    {
        private IUnityContainer broker;
        private IConnectivity connectivity;

        public IUnityContainer Broker
        {
            get { return broker; }
        }

        public IConnectivity Connectivity
        {
            get
            {
                if (connectivity == null)
                {
                    connectivity = Broker.Resolve<IConnectivity>();
                    connectivity = Broker.BuildUp<IConnectivity>(connectivity);
                }
                return connectivity;
            }
        }

        public TestBase()
        {
            InitializeTest();
        }

        private void InitializeTest()
        {
            if (broker != null) return;
            
            broker = new UnityContainer();
            var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Containers["Connectivity"].Configure(broker);
        }
    }
}
